/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.shopping.remote.RemoteCartOrderServiceImpl
   Module Description   :

   Date Created      : 2010/6/14
   Original Author   : Andy.Chuang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.shopping.remote;

import java.util.Calendar;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.ec.shopping.entity.CartOrders;
import com.yaodian100.ec.shopping.service.CartOrdersService;

/**
 * @author Andy.Chuang
 *
 */
public class RemoteCartOrderServiceImpl implements RemoteCartOrderService {
    @Resource
    private BaseManager baseMgr;
    @Resource
    private CartOrdersService cartOrdersService;
    /* (non-Javadoc)
     * @see com.yaodian100.ec.shopping.remote.RemoteCartOrderService#cleanCartOrder30Days(int)
     */
    @Override
    public String cleanCartOrder30Days(Integer batchSize) {
	
	try {
	    Calendar lastMonth=Calendar.getInstance();
	    lastMonth.add(Calendar.DAY_OF_MONTH, -30);
	    DetachedCriteria dc=DetachedCriteria.forClass(CartOrders.class);
	    dc.add(Restrictions.le("modifyDate", lastMonth.getTime()));
	    Object[] cos=null;
	    if(batchSize==null || batchSize==-1){
	        cos=baseMgr.findByCriteria(dc);
	    }else{
	        cos=baseMgr.findByCriteria(dc, 0, batchSize);
	    }
	    if(cos!=null){
	        for(int i=0;i<cos.length;i++){
	            try{
	    		CartOrders co=(CartOrders)cos[i];
	    		cartOrdersService.cleanCartOrder30Days(co.getCartId());
	            }catch(Exception e){
	        	
	        	e.printStackTrace();
	            }
	        }
	       
	    }
	} catch (Exception e) {
	    StackTraceElement[] stes=e.getStackTrace();
	    StringBuffer sb=new StringBuffer("");
	    sb.append("message:"+e.getMessage()+"\r\n");
	    for(StackTraceElement ste:stes){
		sb.append(ste.toString()+"\r\n");
	    }
	    e.printStackTrace();
	    return sb.toString();
	}
	return "true";
    }

}
